<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 用户角色权限模型
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-7-30下午4:32:06
// +----------------------------------------------------------------------

class Authgroup_model extends CI_Model{
	
	const TYPE_ADMIN                = 1;                   // 管理员用户组类型标识
	const MEMBER                    = 'member';
	const UCENTER_MEMBER            = 'ucenter_member';
	const AUTH_GROUP_ACCESS         = 'auth_group_access'; // 关系表表名
	const AUTH_EXTEND               = 'auth_extend';       // 动态权限扩展信息表
	const AUTH_GROUP                = 'auth_group';        // 用户组表名
	const AUTH_EXTEND_CATEGORY_TYPE = 1;              // 分类权限标识
	const AUTH_EXTEND_MODEL_TYPE    = 2; //分类权限标识
	
	
	private $table_name;
	public function __construct(){
		parent::__construct();
		$this->table_name = 'auth_group';
	}
	
	/**
	 * 获取总数
	 * @param array $where 读取条件
	 * @param string $field 字段名
	 */
	public function getCount($where=array(), $field="*"){
		$count =  $this->db->where($where)->select($field)->count_all_results($this->table_name);
		return $count;
	}
	
	/**
	 * 获取角色列表
	 * @param array $where 读取条件
	 * @param string $field 读取字段
	 * @param int $limit 读取条数
	 * @param string $order 排序方式
	 */
	public function getData($where=array(), $field="id,title,description,status", $limit="", $order="id ASC"){
		$res = $this->db->where($where)->select($field)->order_by($order)->limit($limit)->get($this->table_name);
		return $res->result();
	}
	
	/**
	 * 获取字段数据值，只返回一条数据（以数组的形式）
	 * @param unknown_type $where
	 * @param unknown_type $field
	 */
	public function getField($where=array(), $field="id,id,title,rules"){
		$this->db->where($where);
		$this->db->select($field);
		$this->db->limit(1);
		$res = $this->db->get($this->table_name);
		return $res->row_array();
		
		
	}
	
	/**
	 * 添加数据到数据库
	 */
	public function addData(){
		$data_arr = $this->postData();
		$data_arr['model'] = 'admin';
		$query = $this->db->insert($this->table_name, $data_arr);
		return $this->db->insert_id();
	}
	
	/**
	 * 修改数据
	 * @param array/string $where
	 * @author Baip
	 */
	public function updateData($where){
		$data_arr = $this->postData();
		$query = $this->db->where($where)->update($this->table_name, $data_arr);
		return $query;
	}
	
	
	/**
	 * post接收值
	 */
	private function postData(){
		$title  = $this->input->post('title',true);//角色名称
		$description  = $this->input->post('description',true);//角色描述
	
		//产生随机字符串
		$data_arr['title'] = $title;
		$data_arr['description'] = $description;
		return $data_arr;
	}
	
	/**
	 * 获取单条数据
	 * @param unknown_type $where
	 * @param unknown_type $field
	 */
	public function getFind($isobj=1, $where, $field="id,title,description", $limit=1){
		$this->db->where($where);
		$res = $this->db->select($field)->limit($limit)->get($this->table_name);
		return $isobj ? $res->row() : $res->row_array();
	}
	
	/**
	 * 删除数据
	 * @param string $map_value 要删除的执行条件的值
	 * @param booler $type 是in执行还是=执行
	 * @param string $map_field 执行条件的字段
	 */
	public function deleteData($map_value, $type="", $map_field="id"){
		switch ($type){
			case 1:
				$this->db->where(array($map_field,$map_value));
				break;
			case 2:
				$this->db->where_in($map_field, $map_value);
				break;
		}
		$query = $this->db->delete($this->table_name);
		return $query;
	}
	
}